package com.example.yupao.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yupao.common.Result;
import com.example.yupao.enums.StatusEnum;
import com.example.yupao.exception.BusinessException;
import com.example.yupao.model.domain.Team;
import com.example.yupao.model.domain.User;
import com.example.yupao.model.domain.UserTeam;
import com.example.yupao.model.request.*;
import com.example.yupao.model.request.TeamQuitRequest;
import com.example.yupao.model.vo.TeamVO;
import com.example.yupao.model.vo.UserVO;
import com.example.yupao.service.TeamService;
import com.example.yupao.service.UserService;
import com.example.yupao.service.UserTeamService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author hhf
 * @Date 2024/9/18
 * @Description
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Resource
    private TeamService teamService;


    @PostMapping
    public Result addTeam(@RequestBody TeamAddRequest teamAddRequest, HttpServletRequest request) {

        if (teamAddRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR, "参数错误");
        }

        boolean result = teamService.addTeam(teamAddRequest, request);
        return Result.success(result, "新建队伍成功");
    }


    @GetMapping
    public Result searchTeam(TeamSearchRequest teamSearchRequest, HttpServletRequest request) {
        if (teamSearchRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR, "参数错误");
        }
        List<TeamVO> teamList = teamService.searchTeam(teamSearchRequest, request);
        return Result.success(teamList);
    }

    @GetMapping("/page")
    public Result searchTeamPage(TeamSearchRequest teamSearchRequest, HttpServletRequest request) {
        if (teamSearchRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR, "参数错误");
        }
        Page<TeamVO> teamList = teamService.searchTeamPage(teamSearchRequest, request);
        return Result.success(teamList);
    }

    @GetMapping("/my/create")
    public Result searchTeamMyCreate(TeamSearchRequest teamSearchRequest, HttpServletRequest request) {
        if (teamSearchRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR, "参数错误");
        }
        List<TeamVO> teamList = teamService.searchTeam(teamSearchRequest, request);
        return Result.success(teamList);
    }

    @GetMapping("/my/join")
    public Result searchTeamMyJoin(HttpServletRequest request) {
        return Result.success(teamService.searchTeamMyJoin(request));
    }


    @DeleteMapping
    public Result removeTeam(@RequestBody TeamQuitRequest teamQuitRequest, HttpServletRequest request) {
        if (teamQuitRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR, "参数错误");
        }
        boolean result = teamService.removeTeam(teamQuitRequest, request);
        return Result.success(result);
    }

    @DeleteMapping("/quit")
    public Result quitTeam(@RequestBody TeamQuitRequest teamQuitRequest, HttpServletRequest request) {
        if (teamQuitRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR, "参数错误");
        }
        boolean result = teamService.quitTeam(teamQuitRequest, request);
        return Result.success(result);
    }

    @PutMapping
    public Result updateTeam(@RequestBody TeamAddRequest teamAddRequest, HttpServletRequest request) {
        if (teamAddRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR, "参数错误");
        }
        boolean result = teamService.updateTeam(teamAddRequest, request);
        return Result.success(result);

    }

    @PostMapping("/join")
    public Result joinTeam(@RequestBody TeamJoinRequest teamJoinRequest, HttpServletRequest request) {
        if (teamJoinRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR, "参数错误");
        }
        boolean result = teamService.joinTeam(teamJoinRequest, request);
        return Result.success(result);
    }


}
